package S15.second;

import java.util.*;
import java.util.stream.Collectors;

public class S02 {

	public static void main(String[] args) {
		List<Student> list = new ArrayList<>();
        list.add(new Student("张三", 20, 85, "一班"));
        list.add(new Student("李四", 21, 90, "二班"));
        list.add(new Student("王五", 20, 78, "一班"));
        list.add(new Student("杨柳", 21, 88, "二班"));
        list.add(new Student("朝气", 20, 92, "二班"));
        System.out.println("姓名拼接成字符串：");
        String r = list.stream().map(s->s.getName()).collect(Collectors.joining());
        System.out.println(r);
        r=list.stream().map(s->s.getName()).collect(Collectors.joining(", "));
        System.out.println(r);
        r=list.stream().map(s->s.getName())
        	.collect(Collectors.joining(", ","学生名单：[", "]"));
        System.out.println(r);
        
        System.out.println("按不同年龄将学生分组:");
        Map<Integer, List<Student>>map = list.stream().collect(Collectors.groupingBy(s->s.getAge()));
        map.forEach((age, ss)->{
        	System.out.println("年龄："+age+"岁：");
        	ss.forEach(s->System.out.println("\t"+s.getName()+", "+s.getAge()));
        });
        
        System.out.println("统计不同年龄的学生人数:");
        Map<Integer, Long> map2 = list.stream().collect(Collectors.groupingBy(s->s.getAge(), Collectors.counting()));
        map2.forEach((age, count)->{
        	System.out.println(age+"  ---->  "+count);
        });
        
        System.out.println("统计不同年龄中各班级学生人数:");
        Map<Integer, Map<String, Long>> map3 = list.stream().collect(Collectors.groupingBy(s->s.getAge(), Collectors.groupingBy(Student::getClassName, Collectors.counting())));
        map3.forEach((age,map4)->{
        	System.out.println("年龄："+age+"岁：");
        	map4.forEach((bj,count)->System.out.println("\t"+bj+"--->"+count));
        });
        
        System.out.println("统计各班级中不同年龄成绩的平均值:");
        Map<String, Map<Integer, Double>> map4 = list.stream().collect(Collectors.groupingBy(Student::getClassName, Collectors.groupingBy(Student::getAge, Collectors.averagingInt(Student::getScore))));
        map4.forEach((bj,map5)->{
        	System.out.println("班级："+bj);
        	map5.forEach((age,avg)->System.out.printf("\t%d   %.1f\n", age, avg));
        });
	}

}
